﻿using System;
using System.Globalization;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using OpenCvSharp;
using System.Threading;

namespace testyOpenCV
{
  public partial class Form1 : Form
  {
    public ImageBW img;        //obiekt klasy ImageBW(algorytm black-white)posiadajaca pola L i R i inne dane
		public ImageColor img2;    //obiekt klasy ImageColor(algorytm color)posiadajaca pola L i R i inne dane
		public IplImage imgL;
		public IplImage imgR;
    //Algorithm1 pixele;
    //Algorithm2 pixele2;
    //Algorithm3 pixele3;
    //Algorithm4 pixele4;

		public static int dmax;
		public static int winHeight;
		public static int winWidth;
		public static int medWin;
        
    public Form1()
    {
			  
        InitializeComponent();
				//OpenGL_Start();
				thrOpenGL = new Thread(new ThreadStart(OpenGL_Start));
				thrOpenGL.Start();

    }

    //wyswietla zdjecia-----------------------------------SHOW IMAGE
    private void button2_Click(object sender, EventArgs e)
    {
      //------------------------------------------------import image (zamiast osobnego przycisku)
			//dmax = Convert.ToInt16(textBox4.Text.ToString());
			//winHeight = Convert.ToInt16(textBox5.Text.ToString());
			//winWidth = Convert.ToInt16(textBox6.Text.ToString());
			//medWin = Convert.ToInt16(textBox7.Text.ToString());

      string fileNameL = textBox1.Text.ToString();
      string fileNameR = textBox2.Text.ToString();
      imgL = new IplImage(fileNameL);
			imgR = new IplImage(fileNameR);

      using (CvWindow window_lImg = new CvWindow("Left image", imgL))
      using (CvWindow window_rImg = new CvWindow("Right image", imgR))
      {
          CvWindow.WaitKey();
      }
    }
    // przycisk Alg1
    private void button3_Click(object sender, EventArgs e)
    {
      
    }
    // Alg Color
		private void button4_Click(object sender, EventArgs e)
		{
			//------------------------------------------------import image (zamiast osobnego przycisku)
			dmax = Convert.ToInt16(textBox4.Text.ToString());						// przepisanie wartosci dmax do algorytmu
			winHeight = Convert.ToInt16(textBox5.Text.ToString());			// wysokość okna porównywania do alg
			winWidth = Convert.ToInt16(textBox6.Text.ToString());	  		// szerokość okna porównywania do alg
			medWin = Convert.ToInt16(textBox7.Text.ToString());					// wielkość okna filtru medianowego
			string fileNameL = textBox1.Text.ToString();
			string fileNameR = textBox2.Text.ToString();
			//img = new Image(fileNameL, fileNameR, dmax, winHeight, winWidth, medWin);

			try
			{
				img2 = new ImageColor(fileNameL, fileNameR, dmax, winHeight, winWidth, medWin);
				//pixele4 = new Algorithm4(img); //stworzenie obiektu (wykonanie metody) i możliwe odwolywanie sie do pol
			}
			catch (TypeInitializationException ex)
			{
				MessageBox.Show("Nie pobrano zdjęć do przetworzenia", "Błąd.");
			}
		}
		//Alg 3
    private void button5_Click(object sender, EventArgs e)
    {
			
    }
		//Alg BW
    private void button6_Click(object sender, EventArgs e)
    {
			//------------------------------------------------import image (zamiast osobnego przycisku)
			dmax = Convert.ToInt16(textBox4.Text.ToString());						// przepisanie wartosci dmax do algorytmu
			winHeight = Convert.ToInt16(textBox5.Text.ToString());			// wysokość okna porównywania do alg
			winWidth = Convert.ToInt16(textBox6.Text.ToString());	  		// szerokość okna porównywania do alg
			medWin = Convert.ToInt16(textBox7.Text.ToString());					// wielkość okna filtru medianowego
			string fileNameL = textBox1.Text.ToString();
			string fileNameR = textBox2.Text.ToString();
			//img = new Image(fileNameL, fileNameR, dmax, winHeight, winWidth, medWin);
					
      try
      {

				img = new ImageBW(fileNameL, fileNameR, dmax, winHeight, winWidth, medWin);

        //pixele4 = new Algorithm4(img); //stworzenie obiektu (wykonanie metody) i możliwe odwolywanie sie do pol
      }
      catch (TypeInitializationException ex)
      {
        MessageBox.Show("Nie pobrano zdjęć do przetworzenia", "Błąd.");
      }
		}

		private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
		{
			int indexBox1 = comboBox1.SelectedIndex;
			switch (indexBox1)
			{
				case 0:
					textBox1.Text = "tsukubaL.png";
					textBox2.Text = "tsukubaR.png";
					textBox3.Text = "Tsukuba";
					textBox4.Text = "15";			// dmax
					textBox5.Text = "3";			// wysokosc okna porownywania
					textBox6.Text = "3";			// szerokosc okna porownywania
					textBox7.Text = "5";			// median filter window
					break;
				case 1:
					textBox1.Text = "teddyL.png";
					textBox2.Text = "teddyR.png";
					textBox3.Text = "Teddy";
					textBox4.Text = "45";			// dmax
					textBox5.Text = "5";			// wysokosc okna porownywania
					textBox6.Text = "5";			// szerokosc okna porownywania
					textBox7.Text = "7";			// median filter window
					break;
				case 2:
					textBox1.Text = "conesL.png";
					textBox2.Text = "conesR.png";
					textBox3.Text = "Cones";
					textBox4.Text = "15";			// dmax
					textBox5.Text = "3";			// wysokosc okna porownywania
					textBox6.Text = "7";			// szerokosc okna porownywania
					textBox7.Text = "5";			// median filter window
					break;
				case 3:
					textBox1.Text = "venusL.png";
					textBox2.Text = "venusR.png";
					textBox3.Text = "Venus";
					textBox4.Text = "18";			// dmax
					textBox5.Text = "5";			// wysokosc okna porownywania
					textBox6.Text = "7";			// szerokosc okna porownywania
					textBox7.Text = "5";			// median filter window
					break;
				case 4:
					textBox1.Text = "sculptureL.png";
					textBox2.Text = "sculptureR.png";
					textBox3.Text = "Sculpture";
					textBox4.Text = "15";			// dmax
					textBox5.Text = "3";			// wysokosc okna porownywania
					textBox6.Text = "7";			// szerokosc okna porownywania
					textBox7.Text = "5";			// median filter window
					break;
				case 5:
					textBox1.Text = "corridorL.png";
					textBox2.Text = "corridorR.png";
					textBox3.Text = "Corridor";
					textBox4.Text = "12";			// dmax
					textBox5.Text = "3";			// wysokosc okna porownywania
					textBox6.Text = "7";			// szerokosc okna porownywania
					textBox7.Text = "5";			// median filter window
					break;
				case 6:
					textBox1.Text = "ballL.png";
					textBox2.Text = "ballR.png";
					textBox3.Text = "Ball";
					textBox4.Text = "19";			// dmax
					textBox5.Text = "1";			// wysokosc okna porownywania
					textBox6.Text = "5";			// szerokosc okna porownywania
					textBox7.Text = "5";			// median filter window
					break;
				case 7:
					textBox1.Text = "chairL.png";
					textBox2.Text = "chairR.png";
					textBox3.Text = "Chair";
					textBox4.Text = "15";			// dmax
					textBox5.Text = "3";			// wysokosc okna porownywania
					textBox6.Text = "7";			// szerokosc okna porownywania
					textBox7.Text = "5";			// median filter window
					break;
			}
		}

		private void panel1_Paint(object sender, PaintEventArgs e)
		{

		}

		private void Form1_Load(object sender, EventArgs e)
		{
			
		}
		private void OpenGL_Start() { 
			if (this.view.InvokeRequired) {
				System.Threading.ThreadStart aDelegate = new System.Threading.ThreadStart(this.view.Refresh); 
				for( ; ; ) {
					System.Threading.Thread.Sleep(100); 
					if( !thrOpenGL.IsAlive ) 
						return; 
					this.Invoke(aDelegate); 
				} 
			} else { 
				
				for( ; ; ) { 
					// infinity loop for rendering 
					System.Threading.Thread.Sleep(100); 
					this.view.Refresh(); 
				} 
			} 
		}

		private void Form1_FormClosed(object sender, FormClosedEventArgs e)
		{
			try
			{
				thrOpenGL.Abort();
			}
			catch (NullReferenceException exc) { }
			
		}

		public void setOpenGlWindowFocus()
		{
			view.Focus();
		}
  }
}
